package cn.walls1717.mall.modules.pms.service.impl;

import cn.walls1717.mall.modules.pms.dto.PageListDTO;
import cn.walls1717.mall.modules.pms.dto.RelationAttrInfoDTO;
import cn.walls1717.mall.modules.pms.model.PmsProductAttribute;
import cn.walls1717.mall.modules.pms.mapper.PmsProductAttributeMapper;
import cn.walls1717.mall.modules.pms.model.PmsProductAttributeCategory;
import cn.walls1717.mall.modules.pms.service.PmsProductAttributeCategoryService;
import cn.walls1717.mall.modules.pms.service.PmsProductAttributeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 商品属性参数表 服务实现类
 * </p>
 *
 * @author walls1717
 * @since 2022-04-09
 */
@Service
public class PmsProductAttributeServiceImpl extends ServiceImpl<PmsProductAttributeMapper, PmsProductAttribute> implements PmsProductAttributeService {


    private final PmsProductAttributeCategoryService attributeCategoryService;
    private final PmsProductAttributeMapper attributeMapper;

    @Autowired
    public PmsProductAttributeServiceImpl(PmsProductAttributeCategoryService attributeCategoryService, PmsProductAttributeMapper attributeMapper) {
        this.attributeCategoryService = attributeCategoryService;
        this.attributeMapper = attributeMapper;
    }


    @Override
    public Page<PmsProductAttribute> pageList(PageListDTO pageListDTO) {
        LambdaQueryWrapper<PmsProductAttribute> lqw = new LambdaQueryWrapper<>();
        lqw.eq(PmsProductAttribute::getProductAttributeCategoryId, pageListDTO.getCid())
                // 根据类型判断是规格还是参数，0属性规格，1属性参数
                .eq(PmsProductAttribute::getType, pageListDTO.getType())
                .orderByDesc(PmsProductAttribute::getSort);
        Page<PmsProductAttribute> page = new Page<>(pageListDTO.getPageNum(), pageListDTO.getPageSize());
        this.page(page, lqw);
        return page;
    }

    @Override
    public boolean createAttribute(PmsProductAttribute pmsProductAttribute) {
        boolean result = this.save(pmsProductAttribute);
        // 如果添加成功，同步更新商品类型
        if (result) {
            LambdaUpdateWrapper<PmsProductAttributeCategory> luw = new LambdaUpdateWrapper<>();
            if (pmsProductAttribute.getType() == 0) {
                // type = 0 更新属性（属性+1）
                luw.setSql("attribute_count = attribute_count + 1");
            } else if (pmsProductAttribute.getType() == 1) {
                // type = 1 更新参数（参数+1）
                luw.setSql("param_count = param_count + 1");
            }
            luw.eq(PmsProductAttributeCategory::getId, pmsProductAttribute.getProductAttributeCategoryId());
            attributeCategoryService.update(luw);
        }
        return result;
    }

    @Override
    public boolean delete(List<Long> ids) {
        // 先根据前端传回来的id查询到对应的数据
        List<PmsProductAttribute> pas = this.listByIds(ids);
        // 根据id删除这些数据
        boolean result = this.removeByIds(ids);
        // 遍历根据ids拿到的数据，每遍历一次就更新一次pac表
        for (PmsProductAttribute pa : pas) {
            if (result) {
                LambdaUpdateWrapper<PmsProductAttributeCategory> luw = new LambdaUpdateWrapper<>();
                if (pa.getType() == 0) {
                    // type = 0 更新属性（属性+1）
                    luw.setSql("attribute_count = attribute_count - 1");
                } else if (pa.getType() == 1) {
                    // type = 1 更新参数（参数+1）
                    luw.setSql("param_count = param_count - 1");
                }
                luw.eq(PmsProductAttributeCategory::getId, pa.getProductAttributeCategoryId());
                attributeCategoryService.update(luw);
            }
        }
        return result;
    }

    @Override
    public List<RelationAttrInfoDTO> getAttrInfo(Integer productCategoryId) {
        return attributeMapper.getAttrInfo(productCategoryId);
    }
}